#-*-coding:utf-8-*-
import pymysql
from pymysql.cursors import DictCursor

class db_mysql:
    def __init__(self):
        self.db=pymysql.connect(host='www.ddch.pw',port=33060,db='12306',user='train',password='yangfan666',charset='utf8mb4',cursorclass=pymysql.cursors.DictCursor)
        self.data=[]
        self.title=[]

    def exe(self,sql,count=0,commit=False):
        c=self.db.cursor()
        if sql[:-1]!=';':sql+=';'
        c.execute(sql)
        if count==0:count=c.rowcount
        r=c.fetchmany(count)
        if commit:
            self.db.commit()
            print('数据已提交')
        if 'select' in sql:
            print(c.rowcount,c.rownumber)
            self.title=r[0].keys()
            for i in self.title:
                print i,
            print '\n'

            for i in r:
                self.data.append(i.values())
                for j in i.values():
                    print j,
                print '\n'
            return r
if __name__=='__main__':
    mysql=db_mysql()
    mysql.exe('select * from train_order',0)